package me.monster.smarttraffic.ui.login;

import java.util.Map;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import me.monster.smarttraffic.SplashActivity;
import me.monster.smarttraffic.base.BasePresenter;
import me.monster.smarttraffic.bo.DataHolder;
import me.monster.smarttraffic.net.RemoteService;
import me.monster.smarttraffic.tool.KvUtil;
import me.monster.smarttraffic.tool.StringUtil;

/**
 * @description
 * @author: Created jiangjiwei in 2020-02-11 14:56
 */
public class LoginPresenter extends BasePresenter<LoginContract.View> implements LoginContract.Presenter {
    public static final int ACCOUNT_EMPTY = 100;
    public static final int PASSWORD_EMPTY = 200;
    public static final int ACCOUNT_SHORT = 300;
    public static final int PASSWORD_SHORT = 400;
    private static final String TAG = "LoginPresenter";

    @Override
    public void login(String account, String password) {
        iView.showLoading();
        if (inputIllegal(account, password)) {
            iView.dismissLoading();
        } else {
            Disposable loginDis = RemoteService.getInstance()
                    .login(account, password)
                    .subscribe(new Consumer<Map<String, Object>>() {
                        @Override
                        public void accept(Map<String, Object> stringObjectMap) throws Exception {
                            String token = String.valueOf(stringObjectMap.get("token"));
                            String avatar = String.valueOf(stringObjectMap.get("avatar"));
                            String quote = String.valueOf(stringObjectMap.get("quote"));
                            KvUtil.getInstance().editor()
                                    .put(SplashActivity.SPK_ACCOUNT, account)
                                    .put(SplashActivity.SPK_TOKEN, token)
                                    .put(SplashActivity.SPK_AVATAR, avatar)
                                    .put(SplashActivity.SPK_QUOTE, quote)
                                    .apply();
                            DataHolder instance = DataHolder.getInstance();
                            instance.setAvatar(avatar);
                            instance.setAccount(account);
                            instance.setToken(token);
                            instance.setQuote(quote);
                            iView.loginResult(true);
                        }
                    }, new Consumer<Throwable>() {
                        @Override
                        public void accept(Throwable throwable) throws Exception {
                            iView.dismissLoading();
                            iView.toast(handleError(throwable));
                        }
                    });
            addDispose(loginDis);
        }
    }


    private boolean inputIllegal(String account, String password) {
        if (StringUtil.isEmpty(account)) {
            iView.showError(ACCOUNT_EMPTY);
            return true;
        }
        if (StringUtil.isEmpty(password)) {
            iView.showError(PASSWORD_EMPTY);
            return true;
        }
        if (account.length() < 4) {
            iView.showError(ACCOUNT_SHORT);
            return true;
        }
        if (password.length() < 4) {
            iView.showError(PASSWORD_SHORT);
            return true;
        }
        return false;
    }
}
